package com.gm.wholesale.module.unit.ui;

import android.text.InputFilter;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.order.api.IOrderApi;
import com.gm.wholesale.module.order.bean.OrderBean;
import com.gm.wholesale.module.unit.api.IUnitApi;
import com.gm.wholesale.module.unit.bean.UnitBean;
import com.gm.wholesale.module.unit.model.UnitModel;
import com.gm.wholesale.view.DeleteFunctionView;

import java.util.List;

import io.reactivex.Observable;

public class UnitListFragment extends BaseGridFragment<UnitBean, BasePageData<UnitBean>> {
    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        setBodybg(getResources().getColor(R.color.page_bg));
        mGridView.setNumColumns(4);
        mGridView.setHorizontalSpacing(10);
        mGridView.setVerticalSpacing(10);
    }

    private DeleteFunctionView mCurrentDeleteFunctionView;

    @Override
    public void onRefreshView() {
        super.onRefreshView();
        hidDeleteFunctionView();
    }

    @Override
    public void onTouchClick() {
        if (mCurrentDeleteFunctionView != null) {
            mCurrentDeleteFunctionView.hidView();
        }
    }

    @Override
    protected BaseAdapter createTempBaseAdapter() {
        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_unit_item) {
            @Override
            public void convert(ViewHolder viewHolder, final UnitBean data) {
                viewHolder.setText(R.id.unitNameTv, data.getName());

                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mCurrentDeleteFunctionView != null) {
                            mCurrentDeleteFunctionView.hidView();
                            mCurrentDeleteFunctionView = null;
                            return;
                        }
                        showUpdateUnitD(data);
                    }
                });
                viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        hidDeleteFunctionView();
                        mCurrentDeleteFunctionView = viewHolder.getView(R.id.deleteFunctionView);
                        mCurrentDeleteFunctionView.showView(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                deleteUnit(data);

                            }
                        });
                        return false;
                    }
                });
            }
        };

    }

    @Override
    public void onGridScrollStateChanged() {
        hidDeleteFunctionView();
    }

    private void hidDeleteFunctionView() {
        if (mCurrentDeleteFunctionView != null) {
            mCurrentDeleteFunctionView.hidView();
            mCurrentDeleteFunctionView = null;
        }
    }

    private void deleteUnit(UnitBean unitBean) {
        UnitModel.deleteUnit((IBaseView) getActivity(), unitBean.getId(), unitBean.getVersion(), new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                ToastUtil.showToast("删除成功");
                refreshData();
            }
        });
    }

    private void showUpdateUnitD(UnitBean unit) {
        InputDialog dialog = DialogUtil.showInputDialog(getActivity(), "编辑单位", unit.getName(), new InputDialog.OnSubMitBtnClickListener() {
            @Override
            public void onSubMitBtnClick(String inputData, InputDialog inputDialog) {
                if (TextUtils.isEmpty(inputData)) {
                    ToastUtil.showToast("单位名称不能为空");
                    return;
                }
                ArrayMap<String, Object> data = new ArrayMap<>();
                data.put("name", inputData);
                data.put("version", unit.getVersion());
                UnitModel.updateUnit((IBaseView) getActivity(), unit.getId(), data, new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        inputDialog.dismiss();
                        ToastUtil.showToast("修改成功");
                        refreshData();
                    }
                });
            }
        });
        dialog.setEdInputFilter(new InputFilter[]{new InputFilter.LengthFilter(8)});
    }

    @Override
    protected HttpServicesHolder<?, BasePageData<UnitBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<IUnitApi, BasePageData<UnitBean>>() {
            @Override
            public Observable<BasePageData<UnitBean>> getHttpObservable(IUnitApi api) {
                return api.getUnits();
            }
        };
    }
}
